//package com.culture.bootdemo.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.SerializationFeature;
//import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
//import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
//import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
//import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
//import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
///**
// * @Author: wenjl
// * @Date: 2021/6/22 16:51
// * @Description: 配置多个redis库
// * =====>   由于使用了多数据源，固不可以使用默认的springboot进行加载初始化了，这里需要使用自定义config来实现。
// */
//@Configuration
//public class RedisMultipleConfig {
//    /**
//     * 配置lettuce连接池
//     * 这里需要配置lettuce连接池–> LettuceConnectionFactory–>RedisTemplate
//     */
//    @Bean
//    @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
//    public GenericObjectPoolConfig redisPool() {
//        return new GenericObjectPoolConfig();
//    }
//
//
//    /**
//     * 配置第一个数据源的
//     */
//    @Bean
//    @ConfigurationProperties(prefix = "spring.redis0")
//    public RedisStandaloneConfiguration redisConfig0() {
//        return new RedisStandaloneConfiguration();
//    }
//
//    @Bean
//    @ConfigurationProperties(prefix = "spring.redis11")
//    public RedisStandaloneConfiguration redisConfig11() {
//        return new RedisStandaloneConfiguration();
//    }
//
//    /**
//     * 配置第一个数据源的连接工厂
//     * 这里注意：需要添加@Primary 指定bean的名称，目的是为了创建两个不同名称的LettuceConnectionFactory
//     */
//    @Primary
//    @Bean("factory0")
//    public LettuceConnectionFactory factory0(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfig0) {
//        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
//        return new LettuceConnectionFactory(redisConfig0, clientConfiguration);
//    }
//
//    @Bean("factory11")
//    public LettuceConnectionFactory factory11(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfig11) {
//        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
//        return new LettuceConnectionFactory(redisConfig11, clientConfiguration);
//    }
//
//    @Primary
//    @Bean(name = "redisTemplate0")
//    @ConditionalOnMissingBean(name = "redisTemplate0")
//    public RedisTemplate<String, Object> redisTemplate0(@Qualifier("factory0") RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<String, Object> redisTemplate = getRedisTemplate();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        return redisTemplate;
//    }
//
//    @Bean(name = "redisTemplate11")
//    @ConditionalOnMissingBean(name = "redisTemplate11")
//    public RedisTemplate<String, Object> redisTemplate11(@Qualifier("factory11") RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<String, Object> redisTemplate = getRedisTemplate();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        return redisTemplate;
//    }
//
//    private RedisTemplate<String, Object> getRedisTemplate() {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
//        RedisSerializer stringSerializer = new StringRedisSerializer();
//        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
//        Jackson2JsonRedisSerializer jacksonSeial = jackson2JsonRedisSerializer();
//        redisTemplate.setKeySerializer(stringSerializer);
//        redisTemplate.setValueSerializer(jacksonSeial);
//        redisTemplate.setHashKeySerializer(stringSerializer);
//        redisTemplate.setHashValueSerializer(jacksonSeial);
//        return redisTemplate;
//    }
//
//    @Bean(name = "springSessionDefaultRedisSerializer")
//    public Jackson2JsonRedisSerializer jackson2JsonRedisSerializer() {
//        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
//        Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        // 指定要序列化的域，field,get和set,以及修饰符范围，ANY是都有包括private和public
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        // 解决jackson2无法反序列化LocalDateTime的问题
//        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//        om.registerModule(new JavaTimeModule());
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jacksonSeial.setObjectMapper(om);
//        return jacksonSeial;
//    }
//
//}
